//
// Created by ASUS on 2024/7/3/星期三.
//

#include <bits/stdc++.h>
using namespace std;

int diameter(vector<vector<int>>& edges){
	int m=edges.size()+1;
	vector<vector<int>> g(m);
	vector<int> deg(m);
	for(auto&e:edges){
		g[e[0]].push_back(e[1]);
		g[e[1]].push_back(e[0]);
		deg[e[0]]++;
		deg[e[1]]++;
	}
	queue<int> q;
	for(int i=0;i<m;i++){
		if(deg[i]==1){
			q.push(i);
		}
	}
	int len=0,nxt=1;
	while(!q.empty()){
		int size=q.size();
		nxt=size;
		len++;
		while(size--){
			int x=q.front();
			q.pop();
			for(auto&y:g[x]){
				if(y!=x){
					deg[y]--;
					if(deg[y]==1){
						q.push(y);
					}
				}
			}
		}
	}
	return 2*max(len-1,0)+nxt-1;
}